package dao

import (
	"gitee.com/wang-xiangyu20000112/vblog/apps/user/database"
	"gitee.com/wang-xiangyu20000112/vblog/apps/user/po"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// 创建用户
func InsertUser(ctx *gin.Context, user *po.User) *gorm.DB {
	return database.DB.WithContext(ctx).Model(&po.User{}).Create(&user)
}

// 删除用户
func DeleteUser(ctx *gin.Context, user *po.User) *gorm.DB {
	return database.DB.WithContext(ctx).Where(&user).Delete(&po.User{})
}

// 修改用户
func UpdateUser(ctx *gin.Context, user *po.User) *gorm.DB {
	return database.DB.WithContext(ctx).Model(&po.User{ID: user.ID}).Updates(&user)
}

// 根据id查询用户
func FindUser(ctx *gin.Context, id any) (*po.User, error) {
	var user po.User
	result := database.DB.WithContext(ctx).Where("id = ?", id).Find(&user)
	if result.RowsAffected == 0 || result.Error != nil {
		return nil, result.Error
	}
	return &user, nil
}
